package com.chenwc.util;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.chenwc.enumeration.LocaleLanguage;
import com.chenwc.enumeration.TranslateSoftware;
import com.chenwc.util.fileutil.WriteFile;
import com.chenwc.util.fileutil.jarfile.Compressor;
import com.chenwc.util.fileutil.jarfile.Decompression;
import com.chenwc.util.fileutil.propertiesfile.PropertiesFileUtil;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 翻译jar包里面的properties文件
 *
 * @author chenwc
 * @package com.chenwc.util
 * @project wso2-sinicization
 * @date 2024/5/25
 */
public class TranslatePropertiesFile {

    private static final Logger log = LoggerFactory.getLogger(TranslatePropertiesFile.class);

    /**
     * 翻译jar包里面的properties文件
     *
     * @param wso2FilePath      wso2文件路径
     * @param outputPath        翻译输出文件路径
     * @param translateSoftware 翻译软件
     * @param localeLanguage    国际化语言
     */
    public static void translateAllJarPropertiesFile(String wso2FilePath, String outputPath, TranslateSoftware translateSoftware, LocaleLanguage localeLanguage) {
        List<String> filelist = Util.searchUiFileList(wso2FilePath);
        String logFilePath = "info.log";
        File logFile = new File(outputPath + File.separator + logFilePath);
        log.info("logFile: {}", logFile.getAbsolutePath());
        List<JarFileSignEntity> jarFileSignEntityList = new ArrayList<>();
        try {
            if (logFile.exists()) {
                WriteFile.write(logFile, null);
            } else {
                FileUtils.touch(logFile);
            }
            //获取所有需要翻译的properties文件
            for (String file : filelist) {
                //需要解压的文件
                File jarFile = new File(file);
                JarFileSignEntity jarFileSign = new JarFileSignEntity();
                jarFileSign.setJarPath(file);
                String originalUrl = jarFile.getName().replace(".jar", "");
                jarFileSign.setOriginalUrl(originalUrl);
                String filePath = outputPath + "\\file\\" + originalUrl;
                jarFileSign.setFilePath(filePath);
                String newFilePath = outputPath + "\\jar\\" + jarFile.getName();
                jarFileSign.setJarNewPath(newFilePath);

                log.debug("JarFileSignEntity: {}", JSON.toJSONString(jarFileSign, JSONWriter.Feature.WriteMapNullValue));
                //解压后文件的输出目录
                File targetDir = new File(filePath);
                if (targetDir.isDirectory()) {
                    FileUtils.deleteDirectory(targetDir);
                } else {
                    FileUtils.deleteQuietly(targetDir);
                }
                Decompression.uncompress(jarFile, targetDir);
                List<String> list = Util.searchResourcesPropertiesFileList(filePath);
                jarFileSign.setResourcePropertiesFileList(list);
                jarFileSignEntityList.add(jarFileSign);
            }
            log.debug("jarFileSignEntityList: {}", JSON.toJSONString(jarFileSignEntityList, JSONWriter.Feature.WriteMapNullValue));
            log.info("开始翻译");
            //遍历所有jar文件
            for (JarFileSignEntity jarFileSignEntity : jarFileSignEntityList) {
                List<String> resourcePropertiesFileList = jarFileSignEntity.getResourcePropertiesFileList();
                for (String resourcePropertiesFile : resourcePropertiesFileList) {
                    //获取属性文件路径和文件名
                    String propertiesFilePath = resourcePropertiesFile.substring(0, resourcePropertiesFile.lastIndexOf("\\"));
                    log.debug("propertiesFilePath: {}", propertiesFilePath);
                    String propertiesFileName = resourcePropertiesFile.substring(resourcePropertiesFile.lastIndexOf("\\") + 1);
                    log.debug("propertiesFileName: {}", propertiesFileName);
                    //新的语言配置文件路径
                    String newPropertiesFileName = propertiesFileName.substring(0, propertiesFileName.lastIndexOf("."))
                            + "_" + localeLanguage.getLocale()
                            + propertiesFileName.substring(propertiesFileName.lastIndexOf("."));
                    log.debug("newPropertiesFileName: {}", newPropertiesFileName);
                    String newPropertiesFilePath = propertiesFilePath + File.separator + newPropertiesFileName;
                    log.debug("newPropertiesFilePath: {}", newPropertiesFilePath);
                    PropertiesFileUtil.translatePropertiesFile(resourcePropertiesFile, newPropertiesFilePath, translateSoftware);
                    WriteFile.write(logFile, resourcePropertiesFile + "\r\n", true);
                }
                Compressor zc = new Compressor(jarFileSignEntity.getJarNewPath());
                zc.compress(jarFileSignEntity.getFilePath());
            }
        } catch (IOException e) {
            log.error("Error occurred: ", e);
        }
    }

    /**
     * 翻译所有i18n的Resources.properties文件
     *
     * @param wso2FilePath      wso2文件路径
     * @param translateSoftware 翻译软件
     * @param localeLanguage    国际化语言
     */
    public static void translateAllPropertiesFile(String wso2FilePath, TranslateSoftware translateSoftware, LocaleLanguage localeLanguage) {

        List<String> resourcePropertiesFileList = Util.searchResourcesPropertiesFileList(wso2FilePath);
        resourcePropertiesFileList = resourcePropertiesFileList.stream().filter(s -> "Resources.properties".equals(new File(s).getName())).collect(Collectors.toList());
        for (String resourcePropertiesFile : resourcePropertiesFileList) {
            //获取属性文件路径和文件名
            String propertiesFilePath = resourcePropertiesFile.substring(0, resourcePropertiesFile.lastIndexOf("\\"));
            log.debug("propertiesFilePath: {}", propertiesFilePath);
            String propertiesFileName = resourcePropertiesFile.substring(resourcePropertiesFile.lastIndexOf("\\") + 1);
            log.debug("propertiesFileName: {}", propertiesFileName);
            //新的语言配置文件路径
            String newPropertiesFileName = propertiesFileName.substring(0, propertiesFileName.lastIndexOf("."))
                    + "_" + localeLanguage.getLocale()
                    + propertiesFileName.substring(propertiesFileName.lastIndexOf("."));
            log.debug("newPropertiesFileName: {}", newPropertiesFileName);
            String newPropertiesFilePath = propertiesFilePath + File.separator + newPropertiesFileName;
            log.debug("newPropertiesFilePath: {}", newPropertiesFilePath);
            PropertiesFileUtil.translatePropertiesFile(resourcePropertiesFile, newPropertiesFilePath, translateSoftware);
        }
    }
}
